import { ref, toRefs, watch } from "vue";
import { treeselect } from "@/api/system/dept";
export function useFactorySelect(factory, workshop, prodLine, process) {
  //   const { factory, workshop, prodLine, process } = reactive(data);
  const factoryOptions = ref([]);
  const workshopOption = ref([]);
  const lineOption = ref([]);
  const processOption = ref([]);
  function getFactoryList() {
    treeselect().then((res) => {
      factoryOptions.value = res.data[0].children;
    });
  }

  watch(
    () => factory,
    (newValue, oldValue) => {
      const result = factoryOptions.value.filter(
        (el) => el.code == factory.value
      );
      workshopOption.value = result.length ? result[0].children : [];
      lineOption.value = [];
      processOption.value = [];
    },
    { deep: true }
  );
  watch(
    () => workshop,
    (newValue, oldValue) => {
      const result = workshopOption.value.filter(
        (el) => el.code == workshop.value
      );
      lineOption.value = result.length ? result[0].children : [];
      processOption.value = [];
    },
    { deep: true }
  );
  watch(
    () => prodLine,
    (newValue, oldValue) => {
      const result = lineOption.value.filter((el) => el.code == prodLine.value);
      processOption.value = result.length ? result[0].children : [];
    },
    { deep: true }
  );

  getFactoryList();
  return {
    factoryOptions,
    workshopOption,
    lineOption,
    processOption,
  };
}
